export const RegionMixin = {

    onLoad() {
        this.fetchProvince()
    },
    data() {
        return {
            ProvinceData: [], // 省份数据
            ProvinceIndex: '',
            selProvince: '', // 选择的省份id
            CityData: [], // 城市
            CityIndex: '',
            selCity: '', // 选择的城市ID
            RegionData: [], // 区域s
            RegionIndex: '',
            selRegion: '', // 选择的区域ID
            latitude: '',
            longitude: ''
        }
    },

    methods: {
        fetchRegionData(id) {
            let _self = this;
            let _data = []
            _self.$http.fetchData({
                url: "/rest/dms/api/region/get-by-parent",
                method: 'get',
                data: {
                    regionId: id || ''
                },
                success(res) {
                    return res
                }
            })
        },
        fetchProvince() {
            let _self = this;
            _self.$http.fetchData({
                url: "/rest/dms/api/region/get-by-parent",
                method: 'get',
                data: {
                    regionId: ''
                },
                success(res) {
                    _self.ProvinceData = res.data
                }
            })

        },
        fetchCity() {
            let _self = this

            _self.$http.fetchData({
                url: "/rest/dms/api/region/get-by-parent",
                method: 'get',
                data: {
                    regionId: _self.selProvince
                },
                success(res) {
                    _self.CityData = res.data
                }
            })

        },
        fetchRegion() {
            let _self = this
            // let res = _self.fetchRegionData(_self.selCity)
            _self.$http.fetchData({
                url: "/rest/dms/api/region/get-by-parent",
                method: 'get',
                data: {
                    regionId: _self.selCity
                },
                success(res) {
                    _self.RegionData = res.data
                }
            })
        },
        confirmProvin(e) {
            let _self = this;
            _self.ProvinceIndex = parseInt(e.detail.value)
            _self.selProvince = _self.ProvinceData[_self.ProvinceIndex].regionId
            _self.CityIndex = ''
            _self.RegionIndex = ''
            _self.selCity = ''
            _self.selRegion = ''
            _self.fetchCity()
        },
        confirmCity(e) {
            let _self = this;
            if (!_self.selProvince) {
                uni.showToast({
                    title: '请先选择省份'
                })
                return
            }
            _self.CityIndex = parseInt(e.detail.value)
            _self.selCity = _self.CityData[_self.CityIndex].regionId
            _self.selRegion = ''
            _self.RegionIndex = ''
            _self.fetchRegion()
        },
        confirmRegion(e) {
            let _self = this;
            if (!_self.selCity) {
                uni.showToast({
                    title: '请先选择城市'
                })
                return
            }
            _self.RegionIndex = parseInt(e.detail.value)
            let _data = _self.RegionData[_self.RegionIndex]
            _self.selRegion = _data.regionId
            _self.latitude = _data.latitude
            _self.longitude = _data.longitude
        }
    }
}
